#version 430

layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexNormal;
layout(location = 2) in vec2 VertexTexcoord;
layout(location = 3) in vec4 BoneIndex;
layout(location = 4) in vec4 BoneWeight;

layout(std140) uniform;

struct DrawInstParams
{
	mat4 Mvp;
	mat4 ProjectionMatrix;
	mat4 ModelViewMatrix;
	mat3 NormalMatrix;
	vec4 Color;
};

uniform ubDrawInstParams
{
	DrawInstParams drawInstParams[100];
};

uniform uBoneTransformMatrix
{
	mat4 boneTransMats[256];
};

struct LightParams
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	vec4 position;
	vec4 halfVector;
	vec3 spotDirection;
	float spotExponent;
	float spotCutoff; // (range: [0.0,90.0], 180.0)
	float spotCosCutoff; // (range: [1.0,0.0],-1.0)
	float constantAttenuation;
	float linearAttenuation;
	float quadraticAttenuation;
};

struct MaterialParams
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	vec4 emission;
	float shininess;
	float align1;
	vec2 textureOffset;
	vec4 idColor;
};

uniform ubLight
{
	int lightIndex;
	MaterialParams materialParams;
	LightParams lightParams[8];
};

const vec4 lightPosition=vec4(1.0f,1.0f,0.2f,1.0f);

out vec2 Texcoord;
out vec3 lightDir;
out vec3 Normal;
out vec4 Color;

void main()
{
	//vec3 normal,lightDir;
	vec4 diffuse;
	float NdotL;
	mat4 btm=boneTransMats[int(BoneIndex.x)];
	gl_Position = drawInstParams[gl_InstanceID].Mvp * btm * vec4(VertexPosition,1);
	Texcoord=VertexTexcoord + materialParams.textureOffset;
	Color=drawInstParams[gl_InstanceID].Color;
	//if (gl_InstanceID>0)
	//{
	//	Color=vec4(1,1,0,1);
	//}
	//else
	//{
	//	Color=vec4(0,1,1,1);
	//}
	if (lightIndex >= 0)
	{
		Normal=normalize(drawInstParams[gl_InstanceID].NormalMatrix * mat3(btm) * VertexNormal);
		//lightDir = normalize(vec3(lightPosition));
	}
}